<!-- HTML header for doxygen 1.8.3.1-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.3.1"/>
<title>AngelScript: How to build a JIT compiler</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<script type="test/javascript" src="touch.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="aslogo_small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">AngelScript
   </div>
  </td>
   <td>        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
</td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.3.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('doc_adv_jit.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Macros</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark">&#160;</span>Pages</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">How to build a JIT compiler </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>AngelScript doesn't provide a built-in JIT compiler, instead it permits an external JIT compiler to be implemented through a public interface.</p>
<p>To use JIT compilation, the scripts must be compiled with a few extra instructions that provide hints to the JIT compiler and also entry points so that the VM will know when to pass control to the JIT compiled function. By default this is turned off, and must thus be turned on by setting the engine property <a class="el" href="angelscript_8h.html#a53c2e8a74ade77c928316396394ebe0fa7ff74f4afa490b55839daaf217cf898c">asEP_INCLUDE_JIT_INSTRUCTIONS</a>.</p>
<p>If the application sets the <a class="el" href="classas_i_j_i_t_compiler.html">JIT compiler</a> with <a class="el" href="classas_i_script_engine.html#aee4f910163604203a27db1ffea3b1c9c">SetJITCompiler</a> AngelScript will automatically invoke it to provide the <a class="el" href="angelscript_8h.html#ad98ca65b7b5e09d50bcc73a5f184c719">JIT functions</a> with each compilation or <a class="el" href="doc_adv_precompile.html">loading of pre-compiled bytecode</a>.</p>
<h1><a class="anchor" id="doc_adv_jit_3"></a>
The structure of the JIT function</h1>
<p>The <a class="el" href="angelscript_8h.html#ad98ca65b7b5e09d50bcc73a5f184c719">JIT compiled function</a> must follow certain rules in order to behave well with the virtual machine. The intention is that the VM will pass the control to the JIT function, and when the execution is to be suspended the JIT function returns the control to the VM, updating the internal state of the VM so that the VM can resume the execution when requested. Each time the JIT function returns control to the VM it must make sure that the <a class="el" href="structas_s_v_m_registers.html">VM registers</a> and stack values have been updated according to the code that was executed.</p>
<p>The byte code will have a special instruction, <a class="el" href="angelscript_8h.html#ab3692c4e5d47fc93f8c9646d1783aef0a6705ee9692b45f118cfe0ea24581fae5">JitEntry</a>, which defines the positions where the VM can pass the control to the JIT function. These are usually placed for every script statement, and after each instruction that calls another function. This implies that the JIT compiled function needs to be able to start the execution at different points based on the argument in the JitEntry instruction. The value of the argument is defined by the JIT compiler and how it is interpreted is also up to the JIT compiler, with the exception of 0 that means that the control should not be passed to the JIT function.</p>
<p>Some byte code instructions are not meant to be converted into native code. These are usually the ones that have a more global effect on the VM, e.g. the instructions that setup a call to a new script function, or that return from a previous instruction. When these functions are encountered, the JIT function should return the control to the VM, and then the VM will execute the instruction.</p>
<p>Other byte code instructions may be partially implemented by the JIT function, for example those that can throw an exception based on specific conditions. One such example is the instructions for divisions, if the divider is 0 the VM will set an exception and abort the execution. For these instructions the JIT compiler should preferrably implement the condition that doesn't throw an exception, and if an exception is to be thrown the JIT function will instead break out to the VM.</p>
<p>The following shows a possible structure of a JIT compiled function:</p>
<pre>
  void jitCompiledFunc(asSVMRegisters *regs, asPWORD jitArg)
  {
    Read desired VM registers into CPU registers.
    Jump to the current position of the function based on the 'jitArg' argument.
  1:
    Execute code in block 1.
    Jump to exit if an illegal operation is done, e.g. divide by zero. 
    Jump to exit if block ends with an instruction that should not be executed by JIT function. 
  2:
    ...
  3:
    ...
  exit:
    Update the VM registers before returning control to VM.
    If necessary the function can invoke the methods of the context informed 
    in the regs, e.g. to suspend the execution, or to set a script exception.
  }
</pre><h1><a class="anchor" id="doc_adv_jit_2"></a>
Traversing the byte code</h1>
<div class="fragment"><div class="line"><span class="keywordtype">int</span> CJITCompiler::CompileFunction(<a class="code" href="classas_i_script_function.html" title="The interface for a script function description.">asIScriptFunction</a> *func, <a class="code" href="angelscript_8h.html#ad98ca65b7b5e09d50bcc73a5f184c719" title="The function signature of a JIT compiled function.">asJITFunction</a> *output)</div>
<div class="line">{</div>
<div class="line">  <span class="keywordtype">bool</span> success = StartNewCompilation();</div>
<div class="line"></div>
<div class="line">  <span class="comment">// Get the script byte code</span></div>
<div class="line">  <a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815" title="32 bit unsigned integer">asUINT</a>   length;</div>
<div class="line">  <a class="code" href="angelscript_8h.html#a5428f0c940201e5f3bbb28304aeb81bc" title="32 bit unsigned integer">asDWORD</a> *byteCode = func-&gt;<a class="code" href="classas_i_script_function.html#afb38e9ba77ce8b49378e43dadd83ef94" title="Returns the byte code buffer and length.">GetByteCode</a>(&amp;length);</div>
<div class="line">  <a class="code" href="angelscript_8h.html#a5428f0c940201e5f3bbb28304aeb81bc" title="32 bit unsigned integer">asDWORD</a> *end = byteCode + length;</div>
<div class="line">  </div>
<div class="line">  <span class="keywordflow">while</span>( byteCode &lt; end )</div>
<div class="line">  {</div>
<div class="line">    <span class="comment">// Determine the instruction</span></div>
<div class="line">    <a class="code" href="angelscript_8h.html#ab3692c4e5d47fc93f8c9646d1783aef0" title="The bytecode instructions used by the VM.">asEBCInstr</a> op = <a class="code" href="angelscript_8h.html#ab3692c4e5d47fc93f8c9646d1783aef0" title="The bytecode instructions used by the VM.">asEBCInstr</a>(*(<a class="code" href="angelscript_8h.html#a48b3da7121b3abb56bff63b3beb0df63" title="8 bit unsigned integer">asBYTE</a>*)byteCode);</div>
<div class="line">    <span class="keywordflow">switch</span>( op )</div>
<div class="line">    {</div>
<div class="line">    <span class="comment">// Translate each byte code instruction into native code.</span></div>
<div class="line">    <span class="comment">// The codes that cannot be translated should return the control</span></div>
<div class="line">    <span class="comment">// to the VM, so that it can continue the processing. When </span></div>
<div class="line">    <span class="comment">// the VM encounters the next JitEntry instruction it will</span></div>
<div class="line">    <span class="comment">// transfer the control back to the JIT function.</span></div>
<div class="line">    ...</div>
<div class="line">    </div>
<div class="line">    <span class="keywordflow">case</span> <a class="code" href="angelscript_8h.html#ab3692c4e5d47fc93f8c9646d1783aef0a6705ee9692b45f118cfe0ea24581fae5" title="If a JIT function is available and the argument is not 0 then call the JIT function.">asBC_JitEntry</a>:</div>
<div class="line">      <span class="comment">// Update the argument for the JitEntry instruction with  </span></div>
<div class="line">      <span class="comment">// the argument that should be sent to the jit function.</span></div>
<div class="line">      <span class="comment">// Remember that 0 means that the VM should not pass</span></div>
<div class="line">      <span class="comment">// control to the JIT function.</span></div>
<div class="line">      <a class="code" href="angelscript_8h.html#aac9eb586274fc44bb1b838d833963996" title="Macro to access the first pointer argument in the bytecode instruction.">asBC_PTRARG</a>(byteCode) = DetermineJitEntryArg();</div>
<div class="line">      <span class="keywordflow">break</span>;</div>
<div class="line">    }</div>
<div class="line">    </div>
<div class="line">    <span class="comment">// Move to next instruction</span></div>
<div class="line">    byteCode += <a class="code" href="angelscript_8h.html#a9f93754a6f4d43118cd0d2b3896875a5" title="Lookup table for determining the size of each type of bytecode instruction.">asBCTypeSize</a>[<a class="code" href="angelscript_8h.html#ac58d23b688ddd6d6e788b034daf25df7" title="Information on each bytecode instruction.">asBCInfo</a>[op].<a class="code" href="structas_s_b_c_info.html#aa0579956f325760177250e0eddd52ab4" title="Instruction argument layout.">type</a>];</div>
<div class="line">  }</div>
<div class="line">  </div>
<div class="line">  <span class="keywordflow">if</span>( success )</div>
<div class="line">  {</div>
<div class="line">    *output = GetCompiledFunction();</div>
<div class="line">    <span class="keywordflow">return</span> 0;</div>
<div class="line">  }</div>
<div class="line">  </div>
<div class="line">  <span class="keywordflow">return</span> -1;</div>
<div class="line">}</div>
</div><!-- fragment --><p>The following macros should be used to read the arguments from the bytecode instruction. The layout of the arguments is determined from the <a class="el" href="angelscript_8h.html#ac58d23b688ddd6d6e788b034daf25df7">asBCInfo</a> array.</p>
<ul>
<li><a class="el" href="angelscript_8h.html#a7b3dbfcc3928ddd853a4ee53cbc13b69">asBC_DWORDARG</a></li>
<li><a class="el" href="angelscript_8h.html#a290586f7a153d5e8717b01680262b667">asBC_INTARG</a></li>
<li><a class="el" href="angelscript_8h.html#a92e1437ea399e8c545e15bffd651f45f">asBC_QWORDARG</a></li>
<li><a class="el" href="angelscript_8h.html#a0183edd413564ff4897eb4a2473d01f6">asBC_FLOATARG</a></li>
<li><a class="el" href="angelscript_8h.html#aac9eb586274fc44bb1b838d833963996">asBC_PTRARG</a></li>
<li><a class="el" href="angelscript_8h.html#a942798ec89a8ac96550523d80570c703">asBC_WORDARG0</a></li>
<li><a class="el" href="angelscript_8h.html#a0a704bf4db31deda2a69d3216312618c">asBC_WORDARG1</a></li>
<li><a class="el" href="angelscript_8h.html#a2287157faea7f6d32b316c17e0858ddf">asBC_SWORDARG0</a></li>
<li><a class="el" href="angelscript_8h.html#a92601565873cf5d29a6876c2638d7fec">asBC_SWORDARG1</a></li>
<li><a class="el" href="angelscript_8h.html#a698449bbbe369b8a479fb0dd82c9b18e">asBC_SWORDARG2</a></li>
</ul>
<p>What each <a class="el" href="angelscript_8h.html#ab3692c4e5d47fc93f8c9646d1783aef0">byte code</a> instruction does is described in <a class="el" href="doc_adv_jit_1.html">Byte code instructions</a>, but the exact implementation of each byte code instruction is best determined from the implementation in the VM, i.e. the asCScriptContext::ExecuteNext method. </p>
</div></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated on Sun Dec 18 2016 12:35:27 for AngelScript by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.3.1 </li>
  </ul>
</div>
</body>
</html>
